<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<link id="easyuiTheme" rel="stylesheet" type="text/css"	href="${pageContext.request.contextPath }/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/js/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/default.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/js/easyui/jquery.easyui.min.js"></script>
<script	src="${pageContext.request.contextPath }/js/easyui/locale/easyui-lang-zh_CN.js"	type="text/javascript"></script>
<script src="${pageContext.request.contextPath }/js/common.js" type="text/javascript"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/ajaxfileupload.js"></script>
</head>
<body>

    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'west',split:false" style="width:250px;padding:5px">
            <ul id="articleCategoryTree" class="easyui-tree" data-options="url:'${pageContext.request.contextPath}/article/cat/list.do',animate: true,method : 'POST'">
            </ul>
        </div>
        <div data-options="region:'center'" style="padding:5px">
            <table class="easyui-datagrid" id="contentList" data-options="toolbar:contentLisHXoolbar,singleSelect:false,collapsible:true,pagination:true,method:'get',pageSize:20,url:'${pageContext.request.contextPath }/admin/content/contentlist.do'">
		    <thead>
		        <tr>
		            <th data-options="field:'id',width:30">ID</th>
		            <th data-options="field:'title',width:120">内容标题</th>
		            <th data-options="field:'subTitle',width:100">内容子标题</th>
		            <th data-options="field:'titleDesc',width:120">内容描述</th>
		            <th data-options="field:'sortid',align:'center',width:40">优先级</th>
		            <th data-options="field:'url',width:200,align:'center',formaHXer:HONGXIN.formatUrl">内容连接</th>
		            <th data-options="field:'pic',width:300,align:'center',formaHXer:HONGXIN.formatUrl">图片</th>
		            <th data-options="field:'updated',width:130,align:'center',formatter:HONGXIN.formatDateTime">更新日期</th>
		            <th data-options="field:'created',width:130,align:'center',formatter:HONGXIN.formatDateTime">创建日期</th>
		        </tr>
		    </thead>
		</table>
        </div>
    </div>
</body>
</html>

<div id="contentAddWindow" class="easyui-window" title="添加内容" data-options="modal:true,closed:true,iconCls:'icon-save',href:'${pageContext.request.contextPath }/admin/content/content_add.do'" style="width:1000px;height:500px;padding:10px;">
</div>

<script type="text/javascript">

	//清空表单
	function clearForm(){
		$('#contentAddForm').form('reset');
	}
	//提交表单
	function submitForm(){
		//有效性验证
		if(!$('#contentAddForm').form('validate')){
			$.messager.alert('提示','表单还未填写完成!');
			return ;
		}
		
		$.post("${pageContext.request.contextPath }/admin/content/contentsave.do",$("#contentAddForm").serialize(), function(data){
			if(data.status == 200){
				$.messager.alert('提示',data.data);
				$("#contentAddWindow").window('close');
				$("#contentList").datagrid("reload");
				
			}else{
				$.messager.alert('提示',data.data);
				$("#contentAddWindow").window('close');
				$("#contentList").datagrid("reload");
			}
		},'json');
	}
	
	function beforeSend() {
		//var h = document.body.clientHeight; 
		var h = $('#contentAddFormDiv').width();
		$("<div class=\"datagrid-mask\"></div>").css({display:"block",width:"100%",height:h}).appendTo($('#contentAddFormDiv')); 
		$("<div class=\"datagrid-mask-msg\"></div>").html("正在上传数据，请稍候。。。").appendTo($('#contentAddFormDiv')).css({display:"block", 
		left:($(document.body).outerWidth(true) - 190) / 2, 
		top:(h - 45) / 2}); 

	}

	function uploadFile(){

		if($('#uploadPic').val() == ''){
			$.messager.alert('提示','您还没有选择要上传的文件！');
			return false;
		}
		beforeSend();
		upload();
	}

	function upload(){
	    $.ajaxFileUpload({
	        url:'${pageContext.request.contextPath }/file/upload.do',//用于文件上传的服务器端请求地址
	        secureuri:true,//是否启用安全提交，一般设置为false
	        fileElementId:'uploadPic',//文件上传控件的id
	        dataType:'text',//服务器返回的数据类型
	        //success: function (data,status){
	        success: function (data,status){
	        	$('.datagrid-mask-msg').remove(); 
	        	$('.datagrid-mask').remove();
				//手动截取字符串,转换为json对象
				var jsonstring = data.substring(data.indexOf('{'),data.lastIndexOf('}') + 1);
				var jsondata = jQuery.parseJSON(jsonstring);
				
				if(jsondata.error == '0'){
					//上传成功后，给表单字段赋值
					$('#pic').val(jsondata.url);
					$.messager.alert('提示','图片上传成功!');
				}else{
					$.messager.alert('提示','图片上传失败!');
				}
				
	
	        },
	        error: function (data,status,e){
	            alert("服务中断或连接超时导致通信失败！");
	        }
	    }
	    )
	    return false;
	}


	$(function(){
		var tree = $("#articleCategoryTree");
		var datagrid = $("#contentList");
		tree.tree({
			onClick : function(node){
				if(tree.tree("isLeaf",node.target)){
					datagrid.datagrid('reload', {
						categoryId :node.id
			        });
				}
			}
		});
	});
	var contentLisHXoolbar = [{
	    text:'新增',
	    iconCls:'icon-add',
	    handler:function(){
	    	var node = $("#articleCategoryTree").tree("getSelected");
	    	if(!node || !$("#articleCategoryTree").tree("isLeaf",node.target)){
	    		$.messager.alert('提示','新增内容必须选择一个内容分类!');
	    		return ;
	    	}
	    	
	    	//创建窗口
	    	$("#contentAddWindow").window({
	    		
	    		//加载时，将内容分类赋值到表单的cid字段
	    		onLoad :function(){
	    			$('#cid').val(node.id);
        		}

	    		}).window("open");
	    	
	    	}
	},{
	    text:'编辑',
	    iconCls:'icon-edit',
	    handler:function(){
	    	var ids = HX.getSelectionsIds("#contentList");
	    	if(ids.length == 0){
	    		$.messager.alert('提示','必须选择一个内容才能编辑!');
	    		return ;
	    	}
	    	if(ids.indexOf(',') > 0){
	    		$.messager.alert('提示','只能选择一个内容!');
	    		return ;
	    	}
	    	
	    	//获取选中行的数据
	    	//创建窗口
	    	$("#contentAddWindow").window({
	    		//加载时，将内容分类赋值到表单的cid字段
	    		onLoad :function(){
					var data = $("#contentList").datagrid("getSelections")[0];
	    			$("#contentAddForm").form("load",data);
        		}
	    		}).window("open");
			
	    	
	    	
	    }
	},{
	    text:'删除',
	    iconCls:'icon-cancel',
	    handler:function(){
	    	var ids = HX.getSelectionsIds("#contentList");
	    	if(ids.length == 0){
	    		$.messager.alert('提示','未选中内容!');
	    		return ;
	    	}
	    	$.messager.confirm('确认','确定删除ID为 '+ids+' 的内容吗？',function(r){
	    	    if (r){
	    	    	var params = {"ids":ids};
	            	$.post("${pageContext.request.contextPath }/admin/content/delete.do",params, function(data){
	        			if(data.status == 200){
	        				$.messager.alert('提示',data.data,undefined,function(){
	        					$("#contentList").datagrid("reload");
	        				});
	        			}else{
	        				$.messager.alert('提示',data.data,undefined,function(){
	        					$("#contentList").datagrid("reload");
	        				});
	        			}
	        		},'json');
	    	    }
	    	});
	    }
	}];
</script>